package com.oromwin.repos.common.helper;

import java.io.File;
import java.io.IOException;

import org.apache.commons.logging.Log;

import com.oromwin.repos.common.Common;
import com.oromwin.repos.common.Logger;
import com.oromwin.repos.common.ReposConfig;
import com.oromwin.repos.common.ReposException;
import com.oromwin.repos.common.Common.FileExt;

public class FSHelper {
    private static Log log = Logger.getLog(DBHelper.class);
    private static long RESERVED = ReposConfig.getInstance().getReposLocalDiskUsageReserved();
    public static boolean mkdir(File file) throws ReposException {
        try {
            file.mkdirs();
            if (log.isDebugEnabled()) log.debug(String.format("Make directory '%s'", file.toString()));
            return true;
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, String.format("Input path is null, {%s}", file));
        } catch (SecurityException e) {
            throw ReposException.create(log, e, String.format("Can't access to path, {%s}", file));
        }
    }
    public static Long df(File file) throws ReposException {
        try {
            Long freeSpace = new Long(file.getUsableSpace() - RESERVED);
            return freeSpace;
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, String.format("Input path is null, {%s}", file));
        } catch (SecurityException e) {
            throw ReposException.create(log, e, String.format("Can't access to path, {%s}", file));
        }    
    }
    public static boolean ls(File file) throws ReposException {
        try {
            return file.exists();
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, String.format("Input path is null, {%s}", file));
        } catch (SecurityException e) {
            throw ReposException.create(log, e, String.format("Can't access to path, {%s}", file));
        }
    }
    public static boolean delete(File file) throws ReposException {
        try {
            file.delete();
            if (log.isDebugEnabled()) log.debug(String.format("Delete '%s'", file.toString()));
            return true;
        } catch (NullPointerException e) {
            throw ReposException.create(log, e, String.format("Input path is null, {%s}", file));
        } catch (SecurityException e) {
            throw ReposException.create(log, e, String.format("Can't access to path, {%s}", file));
        }
    }
    public static boolean mv(String src, String dst) throws NullPointerException, SecurityException {
        File srcFile = new File(src);
        File dstFile = new File(dst);
        return srcFile.renameTo(dstFile);
    }
    public static boolean mv(File src, File dst) throws NullPointerException, SecurityException {
        return src.renameTo(dst);
    }
    public static File mkfile(String path) throws NullPointerException, SecurityException, IOException {
        File file = new File(path);
        file.createNewFile();
        return file;
    }
    public static File mktempfile(String filename) throws NullPointerException, SecurityException, IOException {
        String suffix = Common.Separator.PERIOD + FileExt.TEMPFILE;
        File file = File.createTempFile(filename, suffix);
        file.createNewFile();
        return file;
    }
}
